package com.rs.service.impl;

import com.rs.config.CurrentUser;
import com.rs.dao.ExamStudentRelationDAO;
import com.rs.dao.ExamWorkDAO;
import com.rs.dao.StudentsDao;
import com.rs.entity.*;
import com.rs.entity.Class;
import com.rs.entity.vo.ExamWorkVO;
import com.rs.service.ExamWorkService;
import com.rs.service.StudentService;
import com.rs.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;


@Service
public class ExamWorkServiceImpl implements ExamWorkService {
    @Autowired
    private ExamWorkDAO examWorkDAO;
    @Autowired
    private ExamStudentRelationDAO examStudentRelationDAO;
    @Autowired
    private StudentsDao studentsDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(ExamWorkVO examWorkVO) {


        try {
            //保存试卷
            ExamWork examWork = new ExamWork();
            examWork.setName(examWorkVO.getName());
            examWork.setBeforeSize(examWorkVO.getBeforeSize());
            examWork.setBeforeTime(DateUtil.getDateBySecond(examWorkVO.getStartTime()+":00",-examWork.getBeforeSize()*60));
            examWork.setInterestId(examWorkVO.getInterest().split(",")[0]);
            examWork.setInterestName(examWorkVO.getInterest().split(",")[1]);
            examWork.setRemark1(examWorkVO.getClassKc().split(",")[0]);
            examWork.setRemark2(examWorkVO.getClassKc().split(",")[1]);
            examWork.setDesc(examWorkVO.getDesc());
            examWork.setUrl(examWorkVO.getUrls());
            examWork.setStartTime(examWorkVO.getStartTime());
            examWork.setEndTime(examWorkVO.getEndTime());
            examWork.setType(examWorkVO.getType());
            examWork.setCreateUser(CurrentUser.getUserName());
            examWork.setCreateId(CurrentUser.getUserId().toString());
            examWork.setCreateTime(DateUtil.getDateFormatter());
            examWork.setRemark3(getNum());
            String urls = examWorkVO.getUrls();
            String names=examWorkVO.getUrlsName();
            examWorkDAO.insertSelective(examWork);

            //查询该课程下的学生
            List<String> students=studentsDao.getStudentNo(Integer.parseInt(examWorkVO.getClassKc().split(",")[0]));
            String sjUrls="";
            if(!StringUtils.isEmpty(urls)){
                sjUrls = urls.substring(1);
            }
            String sjNames="";
            if(!StringUtils.isEmpty(names)){
                sjNames = names.substring(1);
            }
            String[] sj = sjUrls.split(",");
            String[] sjName = sjNames.split(",");
            // 学生随机绑定试卷
            Random random = new Random();
            for (String no : students) {
                ExamStudentRelation examStudentRelation = new ExamStudentRelation();
                examStudentRelation.setExamId(examWork.getId());
                examStudentRelation.setNo(no);
                if(!StringUtils.isEmpty(sjUrls)){
                    if (sj.length < 1) {
                        examStudentRelation.setSjUrl("");
                        examStudentRelation.setSj("");
                    } else {
                        int ran=random.nextInt(sj.length);
                        examStudentRelation.setSjUrl(sj[ran]);
                        examStudentRelation.setSj(sjName[ran]);
                    }
                }
                examStudentRelationDAO.insertSelective(examStudentRelation);
            }
            return examWork.getId();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    private String getNum(){
        return String.valueOf((int)((Math.random()*9+1)*10000));
    }

    @Override
    public List<ExamWork> getList(long page, long limit, String name,Integer type){

        return examWorkDAO.getList((page-1)*limit,limit,name,CurrentUser.getUserId(),type);
    }

    @Override
    public Long getSum(String name,Integer type){
        return examWorkDAO.getSum(name,CurrentUser.getUserId(),type);
    }

    @Override
    public List<ExamWork> getInfoByStudentNoAndType(String studentNo, Integer type) {

        List<ExamWork> examWorks = examWorkDAO.selectInfoByStudentNoAndType(studentNo, type);

        return examWorks;
    }

    @Override
    public ExamWork getOne(Integer id){
        return examWorkDAO.selectByPrimaryKey(id);
    }

    @Override
    public List<Map> getKsStudents(Integer id){

        return examWorkDAO.getKsStudents(id);
    }

    @Override
    public void delExam(Integer id){
        examWorkDAO.deleteByPrimaryKey(id);
        //删除考试学生关联表
        ExamStudentRelationExample example = new ExamStudentRelationExample();
        example.createCriteria().andExamIdEqualTo(id);// 条件
        examStudentRelationDAO.deleteByExample(example);
    }

    @Override
    public void updateLoginInfo(Integer examId, String studentNo, String ip) {
        ExamStudentRelationExample relationExample = new ExamStudentRelationExample();
        relationExample.createCriteria().andNoEqualTo(studentNo).andExamIdEqualTo(examId);

        List<ExamStudentRelation> examStudentRelations = examStudentRelationDAO.selectByExample(relationExample);
        if (examStudentRelations != null && examStudentRelations.size() > 0) {
            ExamStudentRelation examStudentRelation = examStudentRelations.get(0);
            examStudentRelation.setLoginIp(examStudentRelation.getLoginIp()+","+ip);
            examStudentRelation.setLoginNum(examStudentRelation.getLoginNum()+1);
            examStudentRelationDAO.updateByPrimaryKey(examStudentRelation);
        }
    }

    @Override
    public void updateLoginStatus(Integer examId, String studentNo,String url) {
        ExamStudentRelationExample relationExample = new ExamStudentRelationExample();
        relationExample.createCriteria().andNoEqualTo(studentNo).andExamIdEqualTo(examId);

        List<ExamStudentRelation> examStudentRelations = examStudentRelationDAO.selectByExample(relationExample);
        if (examStudentRelations != null && examStudentRelations.size() > 0) {
            ExamStudentRelation examStudentRelation = examStudentRelations.get(0);
            examStudentRelation.setFinish(1);
            examStudentRelation.setFileUrl(url);
            examStudentRelationDAO.updateByPrimaryKey(examStudentRelation);
        }
    }

    @Override
    public String getDownUrl(Integer id,String url){
        return examStudentRelationDAO.getDownUrl(id,url);
    }
    @Override
    public String getDownUrlName(Integer id,String url){
        return examStudentRelationDAO.getDownUrlName(id,url);
    }

}
